package com.qdu.controller;

import com.qdu.entity.Commentinfo;
import com.qdu.entity.Houseinfo;
import com.qdu.entity.Userinfo;
import com.qdu.service.CommentService;
import com.qdu.service.HouseService;
import com.qdu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/comments")
public class CommentController{

    @Autowired
    private CommentService commentService;

    @Autowired
    UserService userService;

    @Autowired
    HouseService houseService;

    @PostMapping("/delete")
    public void deleteComment(String cid){
        commentService.deleteComment(cid);
    }

    @PostMapping("/add")
    public Result addComment(@RequestBody Commentinfo record){
        Result result = new Result();
        record.setCid(UUID.randomUUID().toString());
        record.setCdate(new Date());
        commentService.addComment(record);
        System.out.println(record);
        result.setMsg("发布评论成功");
        return result;
    }

    @GetMapping("/getbyuid")
    public List getCommentByUid(String id){
        System.out.println("uid" + id);
        List<Map> info = new ArrayList<>();
        List<Commentinfo> comments = commentService.getCommentByUid(id);
        for(Commentinfo comment : comments){
            HashMap<String, Object> result = new HashMap<>();
            Userinfo userinfo = userService.selectByPrimaryKey(comment.getUid());
            Houseinfo houseinfo = houseService.selectByHid(comment.getHid());
            List<String> imgs=new ArrayList<>();
            String img=houseinfo.getHimg();
            String[] is=img.split("!");
            for (String i:is){
                imgs.add(i);
            }
            result.put("user", userinfo);
            result.put("house", houseinfo);
            result.put("comment",comment);
            result.put("img", imgs.get(0));
            info.add(result);
        }
        return info;
    }

    @GetMapping("/getbyhid")
    public List getCommentByHid(String id){
        System.out.println("getCommentByHid"+id);
        List<Map> info = new ArrayList<>();
        List<Commentinfo> comments = commentService.getCommentByHid(id);
        for(Commentinfo comment : comments){
            HashMap<String, Object> result = new HashMap<>();
            Userinfo userinfo = userService.selectByPrimaryKey(comment.getUid());
            result.put("user", userinfo);
            result.put("comment",comment);
            info.add(result);
        }
        return info;
    }

    @GetMapping("/getbycid")
    public Commentinfo getCommentByCid(String cid){
        return commentService.getCommentByCid(cid);
    }

    /*public void updateCommentBySelective(Commentinfo record){

    }

    public void updateComment(Commentinfo record){

    }*/
}
